package com.example.messagemodule.ui.fragment;

import android.media.MediaPlayer;

import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.messagemodule.R;
import com.shicheng.mediaplayer.media.MediaCallBack;
import com.shicheng.mediaplayer.media.MediaManager;
import com.examination.mlib.baseold.ModuleBaseFragment;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.ToastUtils;
import com.zzhoujay.richtext.RichText;

import java.text.SimpleDateFormat;
import java.util.Timer;


public class HealthConsultNoiceFragment extends ModuleBaseFragment {


    private Handler handler = new Handler();

    private String url;
    private String content;
    private ImageView directionUsingStatus;
    private TextView healthconsultNoiceStartTime;
    private SeekBar healthconsultNoiceSeek;
    private TextView healthconsultNoiceEndTime;
    private TextView healthconsultNoiceContent;

    private boolean isSeekBarChanging;//互斥变量，防止进度条与定时器冲突。
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private Timer timer;
    SimpleDateFormat format;

    @Override
    public int bondLayout() {
        return R.layout.fragment_health_consult_noice;
    }

    @Override
    public void initView() {
        directionUsingStatus = (ImageView) findViewById(R.id.healthconsult_noice_status);
        healthconsultNoiceStartTime = (TextView) findViewById(R.id.healthconsult_noice_startTime);
        healthconsultNoiceSeek = (SeekBar) findViewById(R.id.healthconsult_noice_seek);
        healthconsultNoiceEndTime = (TextView) findViewById(R.id.healthconsult_noice_endTime);
        healthconsultNoiceContent = (TextView) findViewById(R.id.healthconsult_noice_content);


    }

    @Override
    public void initData() {
        MediaManager.builder()
                .setContext(getActivity())
                .setOpenToast(true)
                .setProgressSeek(healthconsultNoiceSeek)
                .setDebugModel(DevicesUtils.isEnvironment())
                .setUrl(url)
                .setMediaListener(new MediaCallBack() {
                    @Override
                    public void progress(int i, int max) {

                    }
                    @Override
                    public void error() {
                        ToastUtils.showShort("语音播放出错");
                    }
                    @Override
                    public void prepare(int duration, String time) {
                        healthconsultNoiceEndTime.setText(format.format(duration) + "");
                        healthconsultNoiceStartTime.setText("00:00");
                    }

                    @Override
                    public void start() {

                    }

                    @Override
                    public void pause() {

                    }

                    @Override
                    public void stop() {

                    }

                    @Override
                    public void completion() {
                        directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_play));
                    }
                })
                .build();
    }

    @Override
    public void initEvent() {

        directionUsingStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playVoice();
            }
        });
    }

    public void setUrl(String url,String content) {
        this.url = url;
        this.content = content;
    }

    public void refreshFragment(){
        RichText.from(content).bind(this)
                .showBorder(false)
                .into(healthconsultNoiceContent);
        initMediaPlayer();
    }

    void initMediaPlayer() {
        initData();
    }


    void playVoice(){
        if (url!=null){
            if (!mediaPlayer.isPlaying()){
                MediaManager.start();//开始播放
            }else{
                MediaManager.pause();
                directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_play));
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        MediaManager.removeAll();
    }
}